﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace Se.Net.Sockets
{
    public class ExSocket
    {
        public Socket Socket;
        /// <summary>
        /// 客户端地址
        /// </summary>
        public IPEndPoint EndPoint;

        /// <summary>
        /// 接收数据的异步
        /// </summary>
        public SocketAsyncEventArgs receiveAsyncArgs;

        /// <summary>
        /// 发送数据的异步
        /// </summary>
        public SocketAsyncEventArgs SendAsyncArgs;
        /// <summary>
        /// Key
        /// </summary>
        public string Key;

        public DateTime LastAccessTime;

        public ExSocket(Socket socket)
        {
            this.Socket = socket;
            this._sendQueue = new ConcurrentQueue<byte[]>();
        }


        private int _isSending;
        private ConcurrentQueue<byte[]> _sendQueue;

        internal bool TrySetSendFlag()
        {
            return Interlocked.CompareExchange(ref this._isSending, 1, 0) == 0;
        }

        internal void ResetSendFlag()
        {
            Interlocked.Exchange(ref this._isSending, 0);
        }

        public void Enqueue(byte[] data)
        {
            this._sendQueue.Enqueue(data);
        }

        public bool TryDequeue(out byte[] data)
        {
            return this._sendQueue.TryDequeue(out data);
        }
    }
}
